//
//  BSDeptTreePopCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/16.
//

import Foundation
import UIKit

class BSDeptTreePopCell: UITableViewCell {
    let arrowIV = UIImageView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal node: Node, curId: Int64? = nil) {
        nameL.text = node.name
        yyL.text = ""
        xwdL.isHidden = node.modal.sfxz == false
        
        if node.id == curId {
            iconIV.image = UIImage(systemName: "checkmark.circle.fill")
            iconIV.tintColor = .primary
        } else {
            iconIV.image = UIImage(systemName: "circle")
            iconIV.tintColor = .hex("#EDEDED")
        }
        
        let inset = 20 * node.depth

        if !node.isLeaf {
            if node.isExpand {
                arrowIV.image = UIImage(systemName: "chevron.down")
            } else {
                arrowIV.image = UIImage(systemName: "chevron.right")
            }
            iconIV.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(inset + 25)
            }
        } else {
            yyL.text = node.modal.sfyy == true ? "营业" : "不营业"
            arrowIV.image = nil
            iconIV.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(inset + 25)
            }
        }
    }
    
    func setupUI() {
        arrowIV.image = UIImage(systemName: "chevron.down")
        arrowIV.tintColor = .hex("#EDEDED")
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp.centerY)
            make.right.equalTo(self.snp.right).offset(-20)
        }
        
        addSubview(iconIV)
        iconIV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(25)
            make.width.height.equalTo(25)
        }
        
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconIV.snp.right).offset(18)
        }
        
        addSubview(yyL)
        yyL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-75)
        }
        
        xwdL.isHidden = true
        addSubview(xwdL)
        xwdL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-45)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#F3F3F3")
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.snp.bottom)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.height.equalTo(0.5)
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        
    }
    
    lazy var iconIV: UIImageView = {
        let l = UIImageView()
        l.tintColor = .hex("#EDEDED")
        return l
    }()
    
    lazy var nameL: UILabel = {
        let l = UILabel()
        l.font = .systemFont(ofSize: 16)
        l.textColor = .hex("#333333")
        return l
    }()
    
    lazy var yyL: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = .primary
        return label
    }()
    
    lazy var xwdL: PaddingLabel = {
        let label = PaddingLabel()
        label.text = "新"
        label.insets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = .white
        label.layer.cornerRadius = 6
        label.backgroundColor = .hex("#FF0000")
        label.layer.masksToBounds = true
        return label
    }()
}
